package com.ggh.jinjilive.views.live.music.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemReverberationBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.views.live.music.mobile.ItemEntity;

import java.util.List;

public class VoiceAdapter extends RecyclerView.Adapter<VoiceAdapter.ItemViewHolder> {

    private List<ItemEntity> itemEntityList;

    public void setAdd(List<ItemEntity> itemEntityList) {
        this.itemEntityList = itemEntityList;
        notifyDataSetChanged();
    }

    private AddItemOnClickListener addItemOnClickListener;

    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemReverberationBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_reverberation, parent, false);
        return new ItemViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        ItemEntity itemEntity = itemEntityList.get(position);
        if(null != itemEntity){
            holder.binding.imgRing.setImageResource(itemEntity.mIconId);
            holder.binding.tvTitle.setText(itemEntity.mTitle);
            if (itemEntity.mIsSelected) {
                holder.binding.imgRing.setBorderWidth(Float.valueOf(SizeUtils.dp2px(2)));
                holder.binding.imgRing.setBorderColor(holder.binding.imgRing.getResources().getColor(R.color.white));
                holder.binding.tvTitle.setTextColor(holder.binding.tvTitle.getResources().getColor(R.color.white));
            }else {
                holder.binding.imgRing.setBorderWidth(Float.valueOf(SizeUtils.dp2px(2)));
                holder.binding.imgRing.setBorderColor(holder.binding.imgRing.getResources().getColor(R.color.transparent));
                holder.binding.tvTitle.setTextColor(holder.binding.tvTitle.getResources().getColor(R.color.color_66FFFFFF));
            }
            holder.binding.llReverberation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(position,itemEntity);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == itemEntityList ? 0 : itemEntityList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemReverberationBinding binding;

        public ItemViewHolder(@NonNull ItemReverberationBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}